use std::rc::Rc;
use std::cell::RefCell;

#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
    pub val: i32,
    pub left: Option<Rc<RefCell<TreeNode>>>,
    pub right: Option<Rc<RefCell<TreeNode>>>,
}

struct Solution;


impl Solution {
    pub fn flip_equiv(root1: Option<Rc<RefCell<TreeNode>>>, root2: Option<Rc<RefCell<TreeNode>>>) -> bool {
        if root1.is_none() && root2.is_none() {
            return true;
        }

        if let Some(root1_rc) = root1 {
            if let Some(root2_rc) = root2 {
                let borrow1 = root1_rc.borrow();
                let borrow2 = root2_rc.borrow();

                if borrow1.val != borrow2.val {
                    // println!("{} {}",root1_rc.borrow().val,root2_rc.borrow().val);
                    return false;
                }

                if Solution::flip_equiv(borrow1.left.clone(), borrow2.right.clone()) {
                    if !Solution::flip_equiv(borrow1.right.clone(), borrow2.left.clone()) {
                        return false;
                    }
                    return true;
                }

                // 没有反转
                return Solution::flip_equiv(borrow1.left.clone(), borrow2.left.clone()) &&
                    Solution::flip_equiv(borrow1.right.clone(), borrow2.right.clone());
            }
        }

        false
    }
}


fn main() {
    Solution::flip_equiv(None, None);
}
